package com.probox.common.entity.service.supplier.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

import com.probox.common.core.constant.UserConstants;
import com.probox.common.core.enums.SupplierIsAuthStatus;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.SecurityUtils;

import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.supplier.vo.SupplierInfoListVo;
import com.probox.common.entity.domain.supplier.vo.SysUserVo;
import com.probox.systemApi.domain.SysUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.supplier.SupplierSysUserMapper;

import com.probox.common.entity.service.supplier.ISysUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 账号管理Service业务层处理
 *
 * @author usoft
 * @date 2021-05-28
 */
@Service
@Transactional
public class SupplierSysUserServiceImpl extends ServiceImpl<SupplierSysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    private SupplierSysUserMapper supplierSysUserMapper;

    @Autowired
    private com.probox.common.entity.service.system.ISysUserService userService;

    /**
     * 查询账号管理
     *
     * @param userId 账号管理ID
     * @return 账号管理
     */
    @Override
    public SysUser selectSysUserById(Long userId) {
        return this.getById(userId);
    }

    /**
     * 查询账号管理列表
     *
     * @param sysUser 账号管理
     * @return 账号管理
     */
    @Override
    public List<SysUserVo> queryList(SysUser sysUser) {


        return null;
    }

    /**
     * 新增账号管理
     *
     * @param user 账号管理
     * @return 结果
     */
    @Override
    public boolean insertSysUser(SysUser user) {
        Long[] roleIds = new Long[1];
        roleIds[0] = 100L;
        user.setRoleIds(roleIds);
        user.setPlatformUid(SecurityUtils.getPlatformId());
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))) {
             throw new BaseException("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        } else if (StringUtils.isNotEmpty(user.getPhonenumber())
                && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            throw new BaseException("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        } else if (StringUtils.isNotEmpty(user.getEmail())
                && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
            throw new BaseException("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        user.setCreateBy(SecurityUtils.getUsername());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        int i = userService.insertUser(user);
        if(i>0) return true;
        else return false;
    }

    /**
     * 修改账号管理
     *
     * @param sysUser 账号管理
     * @return 结果
     */
    @Override
    public boolean updateSysUser(SysUser sysUser) {
        userService.checkUserAllowed(sysUser);
        if (StringUtils.isNotEmpty(sysUser.getPhonenumber()) && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(sysUser))) {
            throw new BaseException("修改用户'" + sysUser.getUserName() + "'失败，手机号码已存在");
        } else if (StringUtils.isNotEmpty(sysUser.getEmail()) && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(sysUser))) {
            throw new BaseException("修改用户'" + sysUser.getUserName() + "'失败，邮箱账号已存在");
        }
        int i = userService.updateSupplier(sysUser);
        if(i > 0) return true;
        else  return false;
    }


}
